from random import randint

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QPushButton, QSpinBox, QHBoxLayout, \
    QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon, QRegExpValidator
from PyQt5.QtCore import QRegExp, Qt
import sys

sys = sys
QApplication = QApplication


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.widget = QWidget()
        self.mainLayout = QVBoxLayout()

        self.setWindowTitle('猜数')
        self.setWindowIcon(QIcon('assets/icon.ico'))

        self.Title = QLabel("猜数字 | by辉月")
        self.Title.setStyleSheet('font-family:"汉仪文黑-85W";font-size:25px;color:#C8C8C8')
        self.mainLayout.addWidget(self.Title)

        self.maxSpinbox = QSpinBox()
        self.maxSpinbox.setRange(2, 32768)
        self.maxSpinbox.setValue(100)

        self.minSpinbox = QSpinBox()
        self.minSpinbox.setRange(1, 32767)
        self.minSpinbox.setValue(1)

        # noinspection PyUnresolvedReferences
        self.maxSpinbox.valueChanged.connect(self.maxChanged)
        # noinspection PyUnresolvedReferences
        self.minSpinbox.valueChanged.connect(self.minChanged)

        self.maxSpinbox.setStyleSheet('font-family:"汉仪文黑-85W";color:#C8C8C8;font-size:16px')
        self.minSpinbox.setStyleSheet('font-family:"汉仪文黑-85W";color:#C8C8C8;font-size:16px')

        self.childLayout1 = QHBoxLayout()
        self.childLayout1.addWidget(self.minSpinbox)
        self.childLayout1.addWidget(self.maxSpinbox)
        self.mainLayout.addLayout(self.childLayout1)

        self.start = QPushButton('开始/重新随机数')
        self.start.setStyleSheet('font-family:"汉仪文黑-85W";color:#C8C8C8;font-size:16px')
        # noinspection PyUnresolvedReferences
        self.start.clicked.connect(self.getRandnum)
        self.mainLayout.addWidget(self.start)

        reg = QRegExp('[0-9]+$')
        validator = QRegExpValidator()
        validator.setRegExp(reg)
        self.ans = QLineEdit()
        self.ans.setPlaceholderText('请输入回答')
        self.ans.setStyleSheet('font-family:"汉仪文黑-85W";color:#C8C8C8;font-size:16px')
        self.ans.setValidator(validator)
        self.ans.setEnabled(False)
        self.mainLayout.addWidget(self.ans)

        self.answer = QPushButton('回答')
        self.answer.setStyleSheet('font-family:"汉仪文黑-85W";color:#C8C8C8;font-size:16px')
        self.answer.setEnabled(False)
        # noinspection PyUnresolvedReferences
        self.answer.clicked.connect(self.answerEvent)
        self.mainLayout.addWidget(self.answer)

        self.tip = QLabel('')
        self.tip.setStyleSheet('font-family:"汉仪文黑-85W";font-size:22px;color:#C8C8C8')
        self.mainLayout.addWidget(self.tip)

        self.times = 0

        self.widget.setLayout(self.mainLayout)
        self.setCentralWidget(self.widget)

        self.setWindowFlags(self.windowFlags() | Qt.MSWindowsFixedSizeDialogHint)

    def getRandnum(self):
        self.num = randint(self.minSpinbox.value(), self.maxSpinbox.value())
        self.min = self.minSpinbox.value()
        self.max = self.maxSpinbox.value()
        self.tip.setText(f'{self.min}~{self.max}')
        self.minSpinbox.setEnabled(False)
        self.maxSpinbox.setEnabled(False)
        self.ans.setEnabled(True)
        self.answer.setEnabled(True)

    def maxChanged(self):
        self.minSpinbox.setMaximum(self.maxSpinbox.value() - 1)

    def minChanged(self):
        self.maxSpinbox.setMinimum(self.minSpinbox.value() + 1)

    def answerEvent(self):
        self.times += 1
        if self.ans.text() == '':
            QMessageBox.critical(self, 'error', '请输入回答！')
        elif self.ans.text() == str(self.num):
            QMessageBox.information(self, 'information', f'恭喜你回答正确， 共用了{self.times}次')
            self.endEvent()
        else:
            if int(self.ans.text()) > self.max or int(self.ans.text()) < self.min:
                QMessageBox.critical(self, 'error', '请正确回答！')
                return
            if int(self.ans.text()) < self.num:
                # QMessageBox.information(self, '提示', '答错了，你的答案小于正确答案')
                self.tip.setText(f'{self.ans.text()}~{self.max}')
                self.min = int(self.ans.text())
            else:
                # QMessageBox.information(self, '提示', '答错了，你的答案大于正确答案')
                self.tip.setText(f'{self.min}~{self.ans.text()}')
                self.max = int(self.ans.text())
        self.ans.setText('')

    def endEvent(self):
        self.times = 0
        self.num = None
        self.min = None
        self.max = None
        self.tip.setText('')
        self.ans.setText('')
        self.minSpinbox.setEnabled(True)
        self.maxSpinbox.setEnabled(True)
        self.ans.setEnabled(False)
        self.answer.setEnabled(False)
